<?php
namespace Admin\Controller;
use Think\Controller;
class UserController extends Controller {

	public function index(){
		$user=M("User");
		if($_GET['username']){
			$map['username'] = array("LIKE","%{$_GET['username']}%");
		}
		$conut=$user->where($map)->count();
		$Page = new \Think\Page($conut,10);
	    $users = $user->where($map)->field('*')->order('id asc')->limit($Page -> firstRow.','.$Page -> listRows) -> select();
	    
	    // var_dump($users);
	    $this->assign('page',$Page->show());
	    $this->assign('users',$users);
		$this->display();

	}

		public function add(){
		if($_POST['sub']){
			// var_dump($_POST);
			if(!(empty($_POST['username'])) && !(empty($_POST['password']))){
				$user=M('User');
				$_POST['password']=$_POST['repassword']=md5($_POST['password']);
				if($_FILES['profile']['error'] == 4){
						$_POST['profile'] = "test.jpg";
					}else{
						$_POST['profile']=$this->upload();
					}
				if($user->create()){
					if($user->add()){
						$this->redirect("User/index");
					}else{
						$this->error("添加失败！");
					}
				}
			}
		}
		$this->display();
	}

	public function mod(){
		$user = M('User');
		$id = $_GET['id'];
		$data = $user->find($id);
		
		if($_POST['sub']){
			if(I('password')){
				if($_POST['password'] == $_POST['repassword']){
					$_POST['password'] = md5($_POST['password']);
				}else{
					$this -> error("两次密码不一致");
				}
			}else{
				unset($_POST['password']);
			}
			if($_FILES['profile']['error'] == 4){
				$_POST['profile'] = $data['profile'];
			}else{
				$_POST['profile'] = $this -> upload();
				
			}
			
			if($user -> create()){
				if($user -> save()){
					$this -> redirect("User/index");
				}else{
					$this -> error("用户修改失败");
				}
			}else{
				$this -> error("用户修改失败");
			}
		}

		$this -> assign("user",$data);
		$this -> display();
	}
	public function del(){
		$user = M("User");
		$id = I('id');
		$map['id']=array("IN",$id);
		if($user->where($map)->delete($id)){
			$this->redirect("User/index");
		}else{
			$this->error("删除失败！");
		}
	}
	public function Verification(){
		$username = $_POST('username');
		$info = D('user')->where("user_name='$username'")->fand();
	$arr = [$username];

	//
	$stmt->execute($arr);

	$res = $stmt->fetch();

	//检测
	if($res['total'] > 0) {
		echo json_encode(['data'=>0, 'msg'=>'用户已经存在']);
	}else{
		echo json_encode(['data'=>1, 'msg'=>'该用户名可用']);
	}
	}
	private function upload(){
		$config = array(
			"maxSize"=>'10240000',
			"exts"   =>array("jpeg","png","gif","jpg"),
			"rootPath"=>"./Public/Upload/"
			);
		$upload = new \Think\Upload($config);
		$info = $upload->uploadOne($_FILES['profile']);
		// var_dump($info);
		if($info){
			$filePath = $info['savepath'].$info['savename'];
			return $filePath;
		}else{
			$this->error($upload->getError());
		}
	}
		private function uppic(){
		$config = array("maxSize" => '102400000',
				  "exts" => array('jpeg','jpg','png','gif'),
				  "rootPath" => "Public/",
				  "savePath" => "Upload/",
		);
		$upload = new \Think\Upload($config);
		return $upload -> uploadOne($_FILES['pic']);
	}
}